/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUInemeanshop;

import components.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import nemeanshop.NemeanShop;
import nemeanshop.Product;

/**
 *
 * @author havardaxelsen
 */
public class GUIProductlist extends NPanel{
    
    
    private String[]            colname     = {"Navn", "Pris", "Test", "Antall"};
    private JScrollPane         scroll;
    private ArrayList<Product>  plist;
    private NProductModel       mod2;
    private NTable              tab;
    private Dimension           dim         = new Dimension(400,400);
    private NemeanShop          ns;
    private Productmenu         menu;
    private JLabel              tot;
    
    public GUIProductlist(NemeanShop ns)
    {   
        setLayout(new BorderLayout());
        this.ns = ns;
        menu    = new Productmenu(ns);
        tot     = new JLabel("0");
        plist   = ns.getProducts();
        mod2    = new NProductModel(plist);
        tab     = new NTable(mod2, ns);
        tab.setFocusable(false);
        tab.setRowHeight(40);
        tab.setDefaultRenderer(String.class, new MyCellRenderer(tab));
        scroll  = new JScrollPane(tab);
        scroll.setFocusable(false);
        setPreferredSize(dim);
        
        this.setMinimumSize(dim);
        this.add(scroll, BorderLayout.CENTER);
        this.add(menu, BorderLayout.SOUTH);
    }
    
    public void update()
    {
        this.mod2.update(ns.getProducts());
    }
    
}


class Productmenu extends NPanel{
    
    
    private NButton     add     = new NButton("Add");
    private NButton     remove  = new NButton("");
    private NButton     change  = new NButton("");
    private NemeanShop  ns;
    
    public Productmenu(final NemeanShop ns){
        this.setLayout(new GridLayout(1,3));
        Frame f = (Frame)this.getParent();
        final AddProduct2 ap  = new AddProduct2(f, true);
        add(add);
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                ap.setVisible(true);
                
                if (ap.getState() == Status.OK)
                    
                {
                    Product p = new Product(ap.getBarcode(), ap.getProductName(), ap.getStockPrice(), ap.getSellPrice(), ap.getQuantity());
            
                    if (ns.addProduct(p))
                    {
                        JOptionPane.showMessageDialog(null, "Produkt lagt til!");
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Kunne ikke legge til produkt!");
                    }
                }
                ap.reset();
           
            }
        });
        add(remove);
        add(change);
        
    }
}